#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if (head == nullptr || head->next == nullptr)
            return head;

        ListNode* tmp, * cur, * next, * ans;
        cur = head;
        next = head->next;
        ans = next;
        while (cur)
        {
            tmp = next->next;
            if (tmp != nullptr)
            {
                next = tmp->next;
            }
            else
            {
                next = nullptr;
            }
            cur->next->next = cur;
            if (next != nullptr)
            {
                cur->next = next;
                cur = tmp;
            }
            else
            {
                cur->next = tmp;
                cur = nullptr;
            }
        }
        return ans;
    }
};